package handler

import (
	"customer/api/requset"
	"customer/consts"
	"customer/internal/dao"
	"customer/internal/model"
	"customer/internal/queue"
	"customer/pkg"
	"fmt"
	"github.com/dgrijalva/jwt-go"
	"github.com/gin-gonic/gin"
	"github.com/share309/gojwt"
	"log"
	"net/http"
	"time"
)

// 注册、登录
func Register(c *gin.Context) {
	var req requset.RegisterReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}

	var user model.User
	dao.GetOneByFields(&model.User{
		Mobile:   req.Mobile,
		Password: pkg.Md5(req.Password),
	}, &user)

	if user.Id == 0 {
		result := dao.Create(&model.User{
			Mobile:   req.Mobile,
			Password: pkg.Md5(req.Password),
			UType:    1,
		})
		if !result {
			c.JSON(http.StatusOK, gin.H{
				"code": 10000,
				"msg":  "注册失败",
				"data": false,
			})
			return
		}
	} else {
		if user.Password != pkg.Md5(req.Password) {
			c.JSON(http.StatusOK, gin.H{
				"code": 10000,
				"msg":  "账号密码错误",
				"data": false,
			})
			return
		}
	}

	//token
	token, err := gojwt.NewJWT(consts.JWT_KEY).CreateToken(gojwt.CustomClaims{
		ID:          user.Id,
		AuthorityId: user.UType,
		StandardClaims: jwt.StandardClaims{
			ExpiresAt: time.Now().Unix() + 86400*7, //7天有效期
		},
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "token生成失败",
			"data": false,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "登录成功",
		"data": token,
	})

}

// 分页展示
func GetUserList(c *gin.Context) {
	var req requset.GetUserListReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}

	pageDto := dao.PageDto{
		Page:     req.Page,
		PageSize: req.PageSize,
	}

	var user []model.User
	paginate, err := dao.Paginate(&model.User{
		Mobile: req.Mobile,
	}, pageDto, &user)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "分页展示失败",
			"data": false,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "分页展示成功",
		"data": paginate,
	})

}

func UploadImage(c *gin.Context) {
	// 单文件
	file, _ := c.FormFile("file")
	log.Println(file.Filename)

	dst := "D:\\gowork\\src\\customer\\static\\upload\\" + file.Filename
	// 上传文件至指定的完整文件路径
	err := c.SaveUploadedFile(file, dst)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "保存文件路径失败",
			"data": false,
		})
		return
	}

	//返回
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "图片上传成功",
		"data": fmt.Sprintf("http://localhost:8080/upload/%s", file.Filename),
	})
}

func SendGroupMessage(c *gin.Context) {
	var req requset.SendGroupMessageReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}

	message := model.SendMessage{
		Title:   req.Title,
		Content: req.Content,
	}
	dao.Create(&message)

	go queue.PublishPub(consts.SEND_MESSAGE, message)

	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "公告发布成功",
		"data": true,
	})

}

func GetUser(c *gin.Context) {
	var req requset.GetUserReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}

	var user []model.User
	dao.GetListByFields(&model.User{
		Mobile: req.Mobile,
	}, &user)

	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "列表展示成功",
		"data": user,
	})

}
